package com.hyf.serviceedu.controller;


import com.hyf.commonutils.fruit;
import com.hyf.serviceedu.entity.EduChapter;
import com.hyf.serviceedu.entity.chapter.chapterOneVo;
import com.hyf.serviceedu.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>

 */
@Api(description="课程管理章节小结")
@RestController
@RequestMapping("/serviceedu/edu-chapter")
//@CrossOrigin// 跨域注解
public class EduChapterController {
    // 章节和小结部分
    @Autowired
    private EduChapterService eduChapterService;

    // 返回课程大纲的列表,根据课程id 进行查询
    @GetMapping("getChapterchapterTwo/{courseId}")
    public fruit getChapterchapterTwo(@PathVariable String courseId) {
           List<chapterOneVo> list=eduChapterService.getChapterchapterTwoCourseId(courseId);

        return fruit.ok().data("allChapterVido",list);
    }

// 添加章节
@ApiOperation(value = "新增章节")
@PostMapping("addChapter")
    public fruit addChapter(
        @ApiParam(name = "chapterVo", value = "章节对象", required = true)
            @RequestBody EduChapter eduChapter){
    eduChapterService.save(eduChapter);

    return fruit.ok();
}

// 先根据id 进行查询

    @ApiOperation(value = "根据ID查询章节")
    @GetMapping("getChapterInfo/{chapterId}")
    public fruit getChapterInfo(
            @ApiParam(name = "chapterId", value = "章节ID", required = true)
            @PathVariable String chapterId){
        EduChapter byIdChapter = eduChapterService.getById(chapterId);
        return fruit.ok().data("Chapter",byIdChapter);

    }

    // 修改章节
    @ApiOperation(value = "修改章节")
    @PostMapping("updateChapter")
    public fruit updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
                return fruit.ok();

    }

    // 删除的方法

    @ApiOperation(value = "删除章节")
    @DeleteMapping("{chapterId}")
    public fruit deleteChapter( @ApiParam(name = "chapterId", value = "章节ID", required = true)
                               @PathVariable String chapterId){
      boolean result= eduChapterService.deleteChapter(chapterId);
        if(result){
            return fruit.ok();
        }else{
            return fruit.error().message("删除失败");
        }

    }
}

